/**
 * 自动通知管理初始化
 */
var table1 = {
    id: "table1",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
var table2 = {
    id: "table2",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};


var ids=new Array();
/**
 * 初始化表格的列
 */
table1.initColumn = function () {
    return [
        // {field: 'selectItem', radio: false},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '单号', field: 'original_num', visible: true, align: 'center', valign: 'middle',
            cellStyle:function(value,d,index){
                if (d.days>=3&&d.days<10) {
                    return {css:{"background-color":"#dddd00"}}
                }else if(d.days>=10){
                    return {css:{"background-color":"#FF6347"}}
                }
                return '';
            }},
        {title: '收货人', field: 'consignee', visible: true, align: 'center', valign: 'middle'},
        {title: '送货地址', field: 'receivingAddress', visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goodsName', visible: true, align: 'center', valign: 'middle'},
        {title: '合计收款', field: '', visible: true, align: 'center', valign: 'middle',
            formatter : function(value,d,index){
                return parseFloat(d.byf)+parseFloat(d.cc)+parseFloat(d.extra_money)+parseFloat(d.tf)+parseFloat(d.hk);
            }
         },
        {title: '回单要求', field: 'receipt', visible: true, align: 'center', valign: 'middle' },
        {title: '入库时间', field: 'createtime', visible: true, align: 'center', valign: 'middle' }
    ];
};
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
//配送出库
table1.theLibrary=function () {


    if(ids.length<=0){
        Feng.error("请双击左侧表格选择一条记录");
        return false;
    }

    var allTableData =$("#table2").bootstrapTable('getData');//获取表格的所有内容行

    var flag = false;
    for( i=0;i<allTableData.length;i++)
    {
        if(allTableData[i].flow_status>2){
            Feng.error("不能选择已出库订单");
            return false;
        }
    }


    var index = layer.open({
        type: 2,
        title: '配送出库',
        area: ['1150px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tOrder/getDistributionOrders?ids=' + ids.join(",")
    });
    layer.full(index);
    this.layerIndex = index;

}

table1.print=function () {
    var w = window.open(Feng.ctxPath + "/tOrder/printLib?ids=" + ids.join(","));
}

//打印出库
table1.printLibrary=function () {
    var w = window.open(Feng.ctxPath + "/tOrder/printLib?ids=" + ids.join(","));

}
table1.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['status'] = $("#status").val()+"";
    table1.table.refresh({query: queryData});
};
function removeAll()
{

    $("#table2").bootstrapTable('removeAll');

}
function onDblClickRow(row)
{
    $("#table1").bootstrapTable('remove',  {field: 'id', values: [row.id]});

    $("#table2").bootstrapTable('insertRow', {index:1, row:row});
    ids.push(row.id);

    var sum=0;
    var selected=$('#table2').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        sum+=(selected[i].byf+selected[i].cc+selected[i].extra_money+selected[i].tf+selected[i].hk);
    }
    $("#hj_div").show();
    $('#hj').html(sum);

}

function removeRow(row)
{
    $("#table2").bootstrapTable('remove', {field: 'id', values: [row.id]});
    $("#table1").bootstrapTable('insertRow', {index:1, row:row});
    ids.remove(row.id);
    var sum=0;
    var selected=$('#table2').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        sum+=(selected[i].byf+selected[i].cc+selected[i].extra_money+selected[i].tf+selected[i].hk);
    }
    $("#hj_div").show();
    $('#hj').html(sum);

}


function loadData(data){
    var selected=$('#table2').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        for(var j=0;j<data.rows.length;j++){
            if(data.rows[j].id==selected[i].id){
                $("#table1").bootstrapTable('remove',  {field: 'id', values: [selected[i].id]});
            }
        }
    }

}


$(function () {
    var defaultColunms = table1.initColumn();
    var mTable = new BSTable(table1.id, "/tOrder/outdisList?flag=1", defaultColunms);
    mTable.setDblClickRow(onDblClickRow);
    mTable.setOnLoadSuccess(loadData);
    mTable.init();
    table1.table = mTable;

    var table = new BSTable(table2.id, "", defaultColunms);
    table.setDblClickRow(removeRow);
    table.init();
    table2.table = table;

    layui.use('layer', function () {

    })

    $('#status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"状态"
    });
});

$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        table1.search();
    }
});
